﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using LH.Model.ST;
using XXX.Bo;
using System.ServiceModel;
using ServiceReference1;
using XXX.Common;
using System.Xml.Linq;

namespace U8COWeb
{
    /// <summary>
    /// 其他入库单单据接口
    /// </summary>
    public static class Rdrecord08DALCO
    {

        /// <summary>
        /// 构建其他入库单的xml
        /// </summary>
        /// <param name="rdrecord"></param>
        /// <returns></returns>
        public static void GetRd08Xml(ST_Rdrecord08 rdrecord, out string xdomhead, out string xdombody)
        {
            //3、构建表头及表体的xml
            System.Xml.Linq.XDocument xhead = new XDocument();
            //添加根节点,表头表体都
            XNamespace n1 = @"schemas - microsoft - com:rowset";
            XNamespace n2 = @"#RowsetSchema";
            XElement rootEle = new XElement(n1 + "data", new XAttribute(XNamespace.Xmlns + "rs", n1.NamespaceName)
    );
            XElement classEle = new XElement(n2 + "row", new XAttribute(XNamespace.Xmlns + "z", n2.NamespaceName)
    );

            Type type = rdrecord.head.GetType();
            //添加属性
            XAttribute attrClass;
            //XAttribute attrClass = new XAttribute("editprop", "A");
            //classEle.Add(attrClass);

            //查询所有必输项
            //sql = string.Format(@"SELECT lower(ccolumn) as ccolumn FROM zdy_inter_tables WHERE ctable = 'SA_Quomain' and badd = 1");
            //DataTable dtadd = DbHelper.Execute(sql).Tables[0];
            //List<string> litRate = dtadd.AsEnumerable().Select(d => d.Field<string>("ccolumn")).ToList();
            //遍历属性值，有值得写过去
            foreach (PropertyInfo p in type.GetProperties())
            {
                
                    if (p.GetValue(rdrecord.head) != null)
                    {
                        attrClass = new XAttribute(p.Name.ToLower(), p.GetValue(rdrecord.head));
                        classEle.Add(attrClass);
                    }
                 
            }
            ////审核标记，写审核人
            //attrClass = new XAttribute("iStatus", "1");
            //classEle.Add(attrClass);
            rootEle.Add(classEle);
            xhead.Add(rootEle);

            System.Xml.Linq.XDocument xbody = new XDocument();
            //添加根节点,表头表体都
            List<ST_Rdrecord08.Body> lst = rdrecord.bodies;
            XElement rootElebody = new XElement(n1 + "data", new XAttribute(XNamespace.Xmlns + "rs", n1.NamespaceName)
            );
            //多行，要循环写
            for (int i = 0; i < lst.Count; i++)
            {
                ST_Rdrecord08.Body rdrecords = lst[i];
                XElement classElebody = new XElement(n2 + "row", new XAttribute(XNamespace.Xmlns + "z", n2.NamespaceName)
            );
                Type typeBody = rdrecords.GetType();
                //添加属性
                //attrClass = new XAttribute("editprop", "A");
                //classElebody.Add(attrClass);

                //遍历属性值，有值得写过去
                foreach (PropertyInfo p in typeBody.GetProperties())
                {
                    if (p.GetValue(rdrecords) != null)
                      {
                        attrClass = new XAttribute(p.Name.ToLower(), p.GetValue(rdrecords));
                        classElebody.Add(attrClass);
                      }
                     
                }

                rootElebody.Add(classElebody);
            }
            xbody.Add(rootElebody);
            xdomhead = xhead.ToString();
            xdombody = xbody.ToString();
        }



        #region 新增其他入库单 
        /// <summary>
        /// 新增其他入库单,返回ok或者error
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="err">错误信息</param>
        /// <param name="id">新增单据的id</param>
        /// <param name="ccode">新增单据的code</param>
        /// <returns></returns>
        public static bool AddRdrecord08(ST_Rdrecord08 entity, out string err,out string id,out string ccode)
        {
            try
            {
                string xdomhead = XmlOperation.GetCOXmlHead<ST_Rdrecord08.Head>(entity.head);
                string xdombody = XmlOperation.GetCOXmlBody<ST_Rdrecord08.Body>(entity.bodies);

                var binding = new BasicHttpBinding();
                binding.MaxReceivedMessageSize = 2147483647;
                // 根据 WebService 的 URL
                var endpoint = new EndpointAddress(CanShu.coUrl);
                // 创建调用接口的工厂，注意这里泛型只能传入接口
                var factory = new ChannelFactory<WebService1Soap>(binding, endpoint);
                // 从工厂获取具体的调用实例
                var callClient = factory.CreateChannel();
                // 调用具体的方法，这个地方就看你都有啥方法啦，自行替换
                //var request = new SendFormatSMSRequest(sendFormatSMSRequestBody);

                var result = callClient.execAsync("ST", "8", "add", "", xdomhead, xdombody, "", "", "").Result;

                var xmlResult = XDocument.Parse(result); //实例化XmlDocument,怎么用这个,网上去查查
                var node = xmlResult.Descendants("return");
                var resultcode = node.FirstOrDefault().Attribute("bRet").Value;
                var resultmes = node.FirstOrDefault().Attribute("sError").Value;
                if (resultcode == "False")
                {
                    id = "";
                    ccode = "";
                    err = resultmes;
                    return false;
                }
                else
                {
                    id = node.FirstOrDefault().Attribute("sRetID").Value;
                    ccode = node.FirstOrDefault().Attribute("sRetCode").Value;
                    err = "";
                    return true;
                }
            }
            catch (Exception ex)
            {
                id = "";
                ccode = "";
                err = ex.Message;
                return false;
            }
        }






        #endregion

        #region 新增其他入库单 
        /// <summary>
        /// 新增其他入库单,返回ok或者error
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="err">错误信息</param>
        /// <param name="id">新增单据的id</param>
        /// <param name="ccode">新增单据的code</param>
        /// <returns></returns>
        public static bool AddRdrecord08(ST_Rdrecord08 entity, string xmlconfig, out string err, out string id, out string ccode)
        {
            try
            {
                string xdomhead = XmlOperation.GetCOXmlHead<ST_Rdrecord08.Head>(entity.head);
                string xdombody = XmlOperation.GetCOXmlBody<ST_Rdrecord08.Body>(entity.bodies);
                //LoggerHelper logger = new LoggerHelper();
                //logger.Debug("", xdomhead);
                //logger.Debug("", xdombody);
                var binding = new BasicHttpBinding();
                binding.MaxReceivedMessageSize = 2147483647;
                binding.OpenTimeout = new TimeSpan(0, 2, 0); // 设置打开超时时间为 5 分钟
                binding.SendTimeout = new TimeSpan(0, 2, 0); // 设置发送超时时间为 5 分钟
                binding.ReceiveTimeout = new TimeSpan(0, 2, 0); // 设置接收超时时间为 5 分钟
                binding.CloseTimeout = new TimeSpan(0, 2, 0); // 设置关闭超时时间为 5 分钟


                //closeTimeout = "00:10:00" openTimeout = "00:10:00" receiveTimeout = "00:10:00" sendTimeout = "00:10:00" allowCookies = "false"
                // 根据 WebService 的 URL
                var endpoint = new EndpointAddress(CanShu.coUrl);
                // 创建调用接口的工厂，注意这里泛型只能传入接口
                var factory = new ChannelFactory<WebService1Soap>(binding, endpoint);
                // 从工厂获取具体的调用实例
                var callClient = factory.CreateChannel();
                // 调用具体的方法，这个地方就看你都有啥方法啦，自行替换
                //var request = new SendFormatSMSRequest(sendFormatSMSRequestBody);

                var result = callClient.execAsync("DP,ST", "8", "add", "", xdomhead, xdombody, "", xmlconfig, "config").Result;

                var xmlResult = XDocument.Parse(result); //实例化XmlDocument,怎么用这个,网上去查查

                bool bret = XmlOperation.GetCoResult("其他入库单新增", "1", out err, out id, out ccode, result);
                //if (!bret)
                //{
                //    LoggerHelper logger = new LoggerHelper();
                //    logger.Debug("", xdomhead);
                //    logger.Debug("", xdombody);
                //    logger.Debug("", xmlconfig);
                //}
                return bret;
            }
            catch (Exception ex)
            {
                id = "";
                ccode = "";
                err = ex.Message;
                return false;
            }
        }






        #endregion



        #region 新增并审核其他入库单 
        /// <summary>
        /// 新增其他入库单,返回ok或者error
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="err">错误信息</param>
        /// <param name="id">新增单据的id</param>
        /// <param name="ccode">新增单据的code</param>
        /// <returns></returns>
        public static bool AddVerifyRdrecord08(ST_Rdrecord08 entity, string xmlconfig, out string err, out string id, out string ccode)
        {
            try
            {
                string xdomhead = XmlOperation.GetCOXmlHead<ST_Rdrecord08.Head>(entity.head);
                string xdombody = XmlOperation.GetCOXmlBody<ST_Rdrecord08.Body>(entity.bodies);
                //LoggerHelper logger = new LoggerHelper();
                //logger.Debug("", xdomhead);
                //logger.Debug("", xdombody);
                var binding = new BasicHttpBinding();
                binding.MaxReceivedMessageSize = 2147483647;
                binding.OpenTimeout = new TimeSpan(0, 2, 0); // 设置打开超时时间为 5 分钟
                binding.SendTimeout = new TimeSpan(0, 2, 0); // 设置发送超时时间为 5 分钟
                binding.ReceiveTimeout = new TimeSpan(0, 2, 0); // 设置接收超时时间为 5 分钟
                binding.CloseTimeout = new TimeSpan(0, 2, 0); // 设置关闭超时时间为 5 分钟
                // 根据 WebService 的 URL
                var endpoint = new EndpointAddress(CanShu.coUrl);
                // 创建调用接口的工厂，注意这里泛型只能传入接口
                var factory = new ChannelFactory<WebService1Soap>(binding, endpoint);
                // 从工厂获取具体的调用实例
                var callClient = factory.CreateChannel();
                // 调用具体的方法，这个地方就看你都有啥方法啦，自行替换
                //var request = new SendFormatSMSRequest(sendFormatSMSRequestBody);

                var result = callClient.execAsync("DP,ST", "8", "add,verify", "", xdomhead, xdombody, "", xmlconfig, "config").Result;

                //var xmlResult = XDocument.Parse(result); //实例化XmlDocument,怎么用这个,网上去查查
                StringBuilder sb = new StringBuilder();
                sb.AppendLine("cSubID=" + "DP,ST");
                sb.AppendLine("cVouchType=" + "8 其他入库单");
                sb.AppendLine("cOptType=" + "add,verify");
                sb.AppendLine("cVouchID=" + "");
                sb.AppendLine("domHead=" + xdomhead);
                sb.AppendLine("domBody=" + xdombody);
                sb.AppendLine("sErrorMsg=" + "");
                sb.AppendLine("domSub=" + xmlconfig);
                sb.AppendLine("other=" + "config");
                sb.AppendLine("CO=" + result.ToString());
                ExLog.WriteTxt("CO", sb.ToString());
                bool bret = XmlOperation.GetCoResult_addverify("其他入库单新增", "1", out err, out id, out ccode, result);
                //if (!bret)
                //{
                //    LoggerHelper logger = new LoggerHelper();
                //    logger.Debug("", xdomhead);
                //    logger.Debug("", xdombody);
                //    logger.Debug("", xmlconfig);
                //}
                return bret;
            }
            catch (Exception ex)
            {
                id = "";
                ccode = "";
                err = ex.Message;
                return false;
            }
        }






        #endregion

        #region 审核/弃审/删除其他入库单
        /// <summary>
        /// 审核/弃审/删除其他入库单
        /// </summary>
        /// <param name="ID">出库单ID</param>
        /// <param name="u8login">登录对象</param>
        /// <param name="conn">数据库连接对象</param>
        /// <param name="errMsg">错误信息</param>
        /// <param name="coperation">操作：delete，verify，unverify</param>
        /// <returns></returns>
        public static bool VerifyRdrecord08(String ID, string coperation,out string err)
        {
            try
            {
                 
                var binding = new BasicHttpBinding();
                binding.MaxReceivedMessageSize = 2147483647;
                binding.OpenTimeout = new TimeSpan(0, 2, 0); // 设置打开超时时间为 5 分钟
                binding.SendTimeout = new TimeSpan(0, 2, 0); // 设置发送超时时间为 5 分钟
                binding.ReceiveTimeout = new TimeSpan(0, 2, 0); // 设置接收超时时间为 5 分钟
                binding.CloseTimeout = new TimeSpan(0, 2, 0); // 设置关闭超时时间为 5 分钟
                // 根据 WebService 的 URL
                var endpoint = new EndpointAddress(CanShu.coUrl);
                // 创建调用接口的工厂，注意这里泛型只能传入接口
                var factory = new ChannelFactory<WebService1Soap>(binding, endpoint);
                // 从工厂获取具体的调用实例
                var callClient = factory.CreateChannel();
                // 调用具体的方法，这个地方就看你都有啥方法啦，自行替换
                //var request = new SendFormatSMSRequest(sendFormatSMSRequestBody);

                var result = callClient.execAsync("DP,ST", "8", coperation, ID.ToString(), "", "", "", "", "").Result;

                var xmlResult = XDocument.Parse(result); //实例化XmlDocument,怎么用这个,网上去查查
                string id = "";
                string ccode = "";
                return XmlOperation.GetCoResult("其他入库单审核", "0", out err, out id, out ccode, result);
            }
            catch (Exception ex)
            {
                
                err = ex.Message;
                return false;
            }
        }
        #endregion

        #region 审核/弃审/删除其他入库单,带config
        /// <summary>
        /// 审核/弃审/删除其他入库单
        /// </summary>
        /// <param name="ID">出库单ID</param>
        /// <param name="u8login">登录对象</param>
        /// <param name="conn">数据库连接对象</param>
        /// <param name="errMsg">错误信息</param>
        /// <param name="coperation">操作：delete，verify，unverify</param>
        /// <returns></returns>
        public static bool VerifyRdrecord08(String ID, string xmlconfig, string coperation, out string err)
        {
            try
            {

                var binding = new BasicHttpBinding();
                binding.MaxReceivedMessageSize = 2147483647;
                binding.OpenTimeout = new TimeSpan(0, 2, 0); // 设置打开超时时间为 5 分钟
                binding.SendTimeout = new TimeSpan(0, 2, 0); // 设置发送超时时间为 5 分钟
                binding.ReceiveTimeout = new TimeSpan(0, 2, 0); // 设置接收超时时间为 5 分钟
                binding.CloseTimeout = new TimeSpan(0, 2, 0); // 设置关闭超时时间为 5 分钟
                // 根据 WebService 的 URL
                var endpoint = new EndpointAddress(CanShu.coUrl);
                // 创建调用接口的工厂，注意这里泛型只能传入接口
                var factory = new ChannelFactory<WebService1Soap>(binding, endpoint);
                // 从工厂获取具体的调用实例
                var callClient = factory.CreateChannel();
                // 调用具体的方法，这个地方就看你都有啥方法啦，自行替换
                //var request = new SendFormatSMSRequest(sendFormatSMSRequestBody);

                var result = callClient.execAsync("DP,ST", "8", coperation, ID.ToString(), "", "", "", xmlconfig, "config").Result;

                //var xmlResult = XDocument.Parse(result); //实例化XmlDocument,怎么用这个,网上去查查
                StringBuilder sb = new StringBuilder();
                sb.AppendLine("cSubID=" + "DP,ST");
                sb.AppendLine("cVouchType=" + "8 其他入库单");
                sb.AppendLine("cOptType=" + coperation);
                sb.AppendLine("cVouchID=" + ID.ToString());
                sb.AppendLine("sErrorMsg=" + "");
                sb.AppendLine("domSub=" + xmlconfig);
                sb.AppendLine("other=" + "config");
                sb.AppendLine("CO=" + result.ToString());
                ExLog.WriteTxt("CO", sb.ToString());
                string id = "";
                string ccode = "";
                return XmlOperation.GetCoResult("其他入库单审核", "0", out err, out id, out ccode, result);
                //var node = xmlResult.Descendants("return");
                //var resultcode = node.FirstOrDefault().Attribute("bRet").Value;
                //var resultmes = node.FirstOrDefault().Attribute("sError").Value;
                //if (resultcode == "False")
                //{

                //    err = resultmes;
                //    return false;
                //}
                //else
                //{

                //    err = "";
                //    return true;
                //}
            }
            catch (Exception ex)
            {

                err = ex.Message;
                return false;
            }
        }
        #endregion

    }
}
